import { fetchAllPermisionAPI, fetchRoleListAPI, findRolePermisionAPI, deleteRoleAPI, findRoleEmplyeesAPI, addRoleAPI,editRoleAPI } from '@/api/system/roleApi';

const actions = {
  /*  发送请求获取角色列表的action */
  async fetchRoleList(context) {
    const { data } = await fetchRoleListAPI();
    context.commit('updateRoleList', data);
  },

  /* 发送请求删除角色的action */
  async deleteRole(context, roleId) {
    const { msg } = await deleteRoleAPI(roleId);
    return msg;
  },

  /* 发送请求获取所有权限的接口 */
  async fetchAllPermision(context) {
    const { data } = await fetchAllPermisionAPI();
    context.commit('updateAllPermision', data);
  },

  /* 发送请求查询当前角色拥有的权限action */
  async findRolePermision(context, roleId) {
    const { data } = await findRolePermisionAPI(roleId);
    return data;
  },

  /* 发送请求查询当前角色拥有的成员action */
  async findRoleEmplyees(context, pageParams) {
    const { data } = await findRoleEmplyeesAPI(pageParams);
    context.commit('updateRoleEmplyees', data);
  },

  /* 添加角色的qction */
  async addRoleAction(context, data) {
    await addRoleAPI(data);
  },

  /* 编辑角色的action */
  async editRoleAction(context, data) {
    await editRoleAPI(data);
  },
};
const mutations = {
  /*  更新角色列表的mutation */
  updateRoleList(state, roleList) {
    state.roleList = roleList;
  },
  /*  更新所有权限列表的mutation */
  updateAllPermision(state, allPermision) {
    state.allPermision = allPermision;
  },
  /* 更新当前角色拥有的成员mutation */
  updateRoleEmplyees(state, roleEmplyeesInfo) {
    state.roleEmplyeesInfo = roleEmplyeesInfo;
  },
};
const state = () => {
  return {
    roleList: [],
    allPermision: [],
    roleEmplyeesInfo: {},
  };
};

const getters = {
  emplyeesList(state) {
    return state.roleEmplyeesInfo.rows;
  },
  total(state) {
    return state.roleEmplyeesInfo.total;
  },
};

export default {
  actions,
  mutations,
  state,
  getters,
  namespaced: true,
};
